const { createLogger, format, transports, addColors } = require('winston')
const { combine, timestamp, label, printf } = format
const moment = require('moment')

const outFormat = printf((info) => {
  const { level, message, label, timestamp } = info
  const time = moment(new Date(timestamp)).format('YYYY-MM-DD HH:mm:ss')
  return `${time} [${label}] ${level}: ${message}`
})

const logger = createLogger({
  level: 'info',
  format: combine(label({ label: 'info' }), timestamp(), outFormat),
  defaultMeta: { service: 'user-service' },
  transports: [
    new transports.File({ filename: 'logs/error.log', level: 'error' }),
    new transports.File({ filename: 'logs/warn.log', level: 'warn' }),
    new transports.File({ filename: 'logs/combined.log' }),
  ],
})

if (process.env.NODE_ENV !== 'production') {
  logger.add(
    new transports.Console({
      format: format.simple(),
    })
  )
}

module.exports = logger
